﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Clothes.IDAL;
using Clothes.Model;

namespace Clothes.DAL
{
    public class BaseDAL<T>:BaseIDAL<T> where T:BaseContext 
    {

        //protected ClothesContext context { get; set; } = new ClothesContext();

        public ClothesContext context
        {
            get {
                return new ClothesContext();
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public bool Delect(int id)
        {
            var con = context;
            var date = con.Set<T>().SingleOrDefault(a => a.Id ==id);
            con.Set<T>().Remove(date);
            var aa = con.SaveChanges();
            return context.SaveChanges() > 0;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public bool Insert(params T[] t)
        {
            var a = context;
            a.Set<T>().AddRange(t);
            var aaa = a.SaveChanges();
            return aaa>0;
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        public List<T> Select(Expression<Func<T, bool>> expression)
        {
            var con = context;
            return con.Set<T>().Where(a => a.Delect != true).Where(expression).ToList();
        }

        /// <summary>
        /// 根据条件查询
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        public T Tiao(Expression<Func<T, bool>> expression)
        {
            var con = context;
            return con.Set<T>().Where(a => a.Delect != true).SingleOrDefault(expression);
        }

        
    }
}
